/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dpy.appmvdpy.Models.Exporters;

import com.dpy.appmvdpy.Facades.PedidosFacade;
import com.dpy.appmvdpy.Models.Pedidos;
import com.dpy.appmvdpy.Models.ProdutosPedidos;
import freemarker.template.SimpleDate;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author ac_psoares
 */
@Component
@Scope(value="request")
public class PedidoExporter extends Exporter<Pedidos> {

    @Autowired
    public PedidosFacade facade;
    
    public PedidoExporter()
    {

    }
    
    public PedidoExporter(File file)
    {
       super(file);
    }
    
    @Override
    public File doExport(List<Pedidos> items) {
        int i = 0;
        int recordSize = 90;
        for(Pedidos p: items)
        {
           
                for(ProdutosPedidos pp: p.getProdutosPedidosCollection())
                {
                     try {
                                DecimalFormat df = new DecimalFormat("#.00"); 
                                bWriter.append(StringUtils.leftPad(String.valueOf(p.getCodigoDPY()),4,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(p.getVendedor().getCodigoDPY(),3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(p.getData().getDate()),2,"0")  + StringUtils.leftPad(String.valueOf(p.getData().getMonth()+1),2,"0") + StringUtils.leftPad(String.valueOf(p.getData().getYear() % 100),2,"0") );
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getItemn()),3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(p.getClientes().getCodigo(), 3, "0"));
                                bWriter.append("##");
                               // bWriter.append(StringUtils.rightPad(p.getObservacao(), 120));
                               // bWriter.append("##");
                                bWriter.append(StringUtils.rightPad(pp.getIdPRODUTOS().getCodigoDPY(), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidade2().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidade4().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidade8().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidade10().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidade12().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidade14().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidade16().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidade18().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidadeP().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidadeM().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidadeG().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidadeGG().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidadeEG().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(String.valueOf(pp.getQuantidadeXG().intValue()), 3,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(StringUtils.remove(pp.getSomaQuantidade().intValue() + "",'.'), 4,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(StringUtils.remove(df.format(pp.getPreco()),'.'), 5,"0"));
                                bWriter.append("##");
                       
                                
                                if (pp.getPrecoTotal() != null)
                                bWriter.append(StringUtils.leftPad(StringUtils.remove(df.format(pp.getPrecoTotal()),'.'), 5,"0"));
                                
                                bWriter.append("##");
                                bWriter.append("N");
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(p.getFormaPagamento().getCodigoDPY(), 2,"0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad("0", 4, "0"));
                                bWriter.append("##");
                                bWriter.append(StringUtils.leftPad(p.getTipoDocumento(), 2, "0"));
                                bWriter.append("##");
                                bWriter.append(" ");
                                bWriter.append("##");
                                bWriter.append(StringUtils.rightPad(p.getObservacao(), 100, " "));
                                bWriter.append("##");
                                bWriter.append(StringUtils.rightPad(pp.getObservacao(), 40, " "));
                           
                                bWriter.append("\r\n");
                                p.setIntegrado(Boolean.TRUE);
                                facade.edit(p);
                                bWriter.flush();
                                
                     } catch (IOException ex) {
                         Logger.getLogger(PedidoExporter.class.getName()).log(Level.SEVERE, null, ex);
                     }
                    
                
                }
          
        }
        return this.out;
    }
    
}
